﻿using System;
using System.Device.Location;

namespace Hibiku.Services
{
    public class WindowsGeoLocationService : IGeoLocatorService
    {
        private readonly GeoCoordinateWatcher _watcher;

        public WindowsGeoLocationService()
        {
            _watcher = new GeoCoordinateWatcher();

            _watcher.PositionChanged += HandlePositionChanged;
        }

        private void HandlePositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (!e.Position.Location.IsUnknown)
            {
                LocationFound(e.Position.Location.Latitude, e.Position.Location.Longitude);
            }

            _watcher.Stop();
        }

        #region IGeoLocatorService Members

        public event Action<double, double> LocationFound = delegate { };

        public void Locate()
        {
            _watcher.Start();
        }

        #endregion
    }
}
